/**
 * Copyright 2006 Van de Wijgerd B.V. All rights reserved.
 */
package com.googlecode.friendfeed;

import com.googlecode.AbstractApiImpl;
import com.googlecode.ApiException;
import com.googlecode.HttpCon;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;

public class FriendFeedApiImpl extends AbstractApiImpl implements FriendFeedApi {
    private String apiUrl = DEFAULT_API_URL;

    public FriendFeedApiImpl(HttpCon httpCon, String nickName, String remoteKey) {
        super(httpCon, new AuthScope("friendfeed.com", 80, AuthScope.ANY_REALM), nickName, remoteKey);
    }

    public FriendFeedApiImpl(HttpClient httpClient) {
        super(httpClient);
    }

    public FriendFeedApiImpl(HttpClient httpClient,String nickName, String remoteKey) {
        super(httpClient,new AuthScope("friendfeed.com", 80, AuthScope.ANY_REALM),nickName,remoteKey);
    }

    public Entries fetchPublicFeed() throws ApiException {
        GetMethod method = new GetMethod(apiUrl+"/feed/public");
        String json = execute(method);
        Entries entries = null;
        try {
            JSONObject jsonObject = JSONObject.fromObject(json);
            entries = (Entries) JSONObject.toBean(jsonObject,Entries.class,Entries.getClassMap());
        } catch (JSONException e) {
            throw new ApiException(json,e);
        }
        entries.setJson(json);
        return entries;
    }

    public Entries fetchUserFeed() throws ApiException {
        if(!hasCredentials()) {
            throw new ApiException(403,"Authentication Required");
        }
        GetMethod method = new GetMethod(apiUrl+"/feed/user/"+credentials.getUserName());
        String json = execute(method);
        JSONObject jsonObject = JSONObject.fromObject(json);
        Entries entries = (Entries) JSONObject.toBean(jsonObject,Entries.class,Entries.getClassMap());
        entries.setJson(json);
        return entries;
    }

    public Entries fetchUserFeed(String nickName) throws ApiException {
        if(!hasCredentials()) {
            throw new ApiException(403,"Authentication Required");
        }
        GetMethod method = new GetMethod(apiUrl+"/feed/user/"+nickName);
        String json = execute(method);
        JSONObject jsonObject = JSONObject.fromObject(json);
        Entries entries = (Entries) JSONObject.toBean(jsonObject,Entries.class,Entries.getClassMap());
        entries.setJson(json);
        return entries;
    }

    public Entries fetchUserCommentedFeed() throws ApiException {
        if(!hasCredentials()) {
            throw new ApiException(403,"Authentication Required");
        }
        GetMethod method = new GetMethod(apiUrl+"/feed/user/"+credentials.getUserName()+"/comments");
        String json = execute(method);
        JSONObject jsonObject = JSONObject.fromObject(json);
        Entries entries = (Entries) JSONObject.toBean(jsonObject,Entries.class,Entries.getClassMap());
        entries.setJson(json);
        return entries;
    }

    public Entries fetchUserLikedFeed() throws ApiException {
        if(!hasCredentials()) {
            throw new ApiException(403,"Authentication Required");
        }
        GetMethod method = new GetMethod(apiUrl+"/feed/user/"+credentials.getUserName()+"/likes");
        String json = execute(method);
        JSONObject jsonObject = JSONObject.fromObject(json);
        Entries entries = (Entries) JSONObject.toBean(jsonObject,Entries.class,Entries.getClassMap());
        entries.setJson(json);
        return entries;
    }

    public Entries fetchUserDiscussedFeed() throws ApiException {
        if(!hasCredentials()) {
            throw new ApiException(403,"Authentication Required");
        }
        GetMethod method = new GetMethod(apiUrl+"/feed/user/"+credentials.getUserName()+"/discussion");
        String json = execute(method);
        JSONObject jsonObject = JSONObject.fromObject(json);
        Entries entries = (Entries) JSONObject.toBean(jsonObject,Entries.class,Entries.getClassMap());
        entries.setJson(json);
        return entries;
    }

    public Entries fetchRoomFeed(String roomName) throws ApiException {
        if(!hasCredentials()) {
            throw new ApiException(403,"Authentication Required");
        }
        GetMethod method = new GetMethod(apiUrl+"/feed/room/"+roomName);
        String json = execute(method);
        JSONObject jsonObject = JSONObject.fromObject(json);
        Entries entries = (Entries) JSONObject.toBean(jsonObject,Entries.class,Entries.getClassMap());
        entries.setJson(json);
        return entries;
    }

    public Profile getProfile() throws ApiException {
        if(!hasCredentials()) {
            throw new ApiException(403,"Authentication Required");
        }
        GetMethod method = new GetMethod(apiUrl+"/user/"+credentials.getUserName()+"/profile");
        String json = execute(method);
        JSONObject jsonObject = JSONObject.fromObject(json);
        Profile profile = (Profile) JSONObject.toBean(jsonObject,Profile.class,Profile.getClassMap());
        profile.setJson(json);
        return profile;
    }

}
